﻿@inject IDataSourceService DataSourceService
@inject IDatabaseService DatabaseService

<MudStack>
    <MudPaper Class="pa-4" Elevation="0">
        <MudText Color="Color.Primary" Typo="Typo.h6">基本信息</MudText>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto">
            <MudGrid>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.SourceCode" For="@(() => _editRequestDto.SourceCode)" Label="数据源编码"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudTextField @bind-Value="_editRequestDto.SourceName" For="@(() => _editRequestDto.SourceName)" Label="数据源名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
                <MudItem xs="6">
                    <MudSelectList TItem="DatabaseInfoResponseDto" TValue="Guid?" Items="_databases"
                                   TextField="@((item) => item.DatabaseName)"
                                   ValueField="@((item) => item.Key)"
                                   @bind-Value="_editRequestDto.DatabaseId" For="@(() => _editRequestDto.DatabaseId)"
                                   Label="数据库" Dense="true" AnchorOrigin="Origin.BottomCenter"
                                   Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudSelectList>
                </MudItem>
                <MudItem xs="6">
                    <MudSelect @bind-Value="_editRequestDto.DatabaseObjectType" For="@(() => _editRequestDto.DatabaseObjectType)" Label="数据来源类型"
                               Dense="true" Clearable AnchorOrigin="Origin.BottomCenter"
                               Variant="Variant.Outlined" Margin="Margin.Dense">
                        <MudSelectItem Value="@("sqlText")">自定义SQL</MudSelectItem>
                        <MudSelectItem Value="@("table")">数据库表</MudSelectItem>
                        <MudSelectItem Value="@("proc")">存储过程</MudSelectItem>
                    </MudSelect>
                </MudItem>
                <MudItem xs="12">
                    <MudField Label="SQL语句" InnerPadding="false" Variant="Variant.Outlined" Margin="Margin.Dense">
                        <StandaloneCodeEditor Id="my-editor-instance-id" ConstructionOptions="EditorConstructionOptions" CssClass="my-editor-class"/>
                    </MudField>
                </MudItem>
                <MudItem xs="12">
                    <MudTextField @bind-Value="_editRequestDto.Description" For="@(() => _editRequestDto.Description)" Label="数据源描述"
                                  Lines="3" Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudTextField>
                </MudItem>
            </MudGrid>
        </MudForm>
    </MudPaper>
    <MudPaper Class="pa-4" Elevation="0">
        <MudStack Spacing="4">
            <MudText Color="Color.Primary" Typo="Typo.h6">设置数据源</MudText>
            <MudText Typo="Typo.subtitle1">1.选择数据字段</MudText>
            <MudStack Row="true">
                @*<MudCheckBox T="bool" Checked="_isAllChecked" Label="全选" Dense="true" CheckedChanged="CheckAllHandler"></MudCheckBox>*@
                <MudText Style="margin-left: 10px; height: 32px; line-height: 32px;">已选（0）/ 共计（0）</MudText>
            </MudStack>
            <MudStack>
                <MudPaper Class="border-solid border-2 mud-border-primary pa-4" Elevation="0">
                    <MudGrid>
                        @*@foreach (var item in _dataSourceFields)
                        {
                        <MudItem xs="2">
                        <MudCheckBox Label="@item.FieldCode" @bind-Checked="@item.IsVisible" Dense="true"></MudCheckBox>
                        </MudItem>
                        }*@
                    </MudGrid>
                </MudPaper>
            </MudStack>
            <MudText Typo="Typo.subtitle1">2.选择业务指标</MudText>
            @*<MudDataGrid T="DataSourceFieldEditDto" Items="_dataSourceFields" Dense="true" FixedHeader="true"
            HorizontalScrollbar="true" Height="400px" ReadOnly="false" EditMode="DataGridEditMode.Cell"
            QuickFilter="@(x=>x.IsVisible)">
            <Columns>
            <Column T="DataSourceFieldEditDto" Field="@(nameof(DataSourceFieldEditDto.FieldCode))" Title="字段名称" IsEditable="false" />
            <Column T="DataSourceFieldEditDto" Field="@(nameof(DataSourceFieldEditDto.FieldDataType))" Title="数据格式">
            <EditTemplate>
            <MudSelect @bind-Value="context.Item.FieldDataType" Dense="true" AnchorOrigin="Origin.BottomCenter">
            <MudSelectItem Value="@("Guid")">Guid</MudSelectItem>
            <MudSelectItem Value="@("String")">文本</MudSelectItem>
            <MudSelectItem Value="@("Number")">数值</MudSelectItem>
            <MudSelectItem Value="@("DateTime")">日期</MudSelectItem>
            <MudSelectItem Value="@("Boolean")">布尔</MudSelectItem>
            </MudSelect>
            </EditTemplate>
            </Column>
            <Column T="DataSourceFieldEditDto" Field="@(nameof(DataSourceFieldEditDto.DisplayName))" Title="显示名称" />
            <Column T="DataSourceFieldEditDto" Field="@(nameof(DataSourceFieldEditDto.Sort))" Title="排序" />
            <Column T="DataSourceFieldEditDto" Field="@(nameof(DataSourceFieldEditDto.FieldDescription))" Title="描述" />
            </Columns>
            </MudDataGrid>*@
        </MudStack>
    </MudPaper>
    <MudPaper Class="pa-4" Elevation="0">
        <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="SubmitHandle">保存</MudButton>
    </MudPaper>
</MudStack>

@code {

    private IList<DatabaseInfoResponseDto> _databases = new List<DatabaseInfoResponseDto>();
    private DataSourceEditRequestDto _editRequestDto = new();

    private DataSourceEditValidator _editValidator = new();

    private MudForm _editRequestForm = new();

    private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
    {
        return new StandaloneEditorConstructionOptions
        {
            Language = "sql",
            GlyphMargin = true,
            Value = "",
            AutomaticLayout = true
        };
    }

    [Parameter]
    public Guid? Key { get; set; }

    void CancelHandle()
    {
    }

    async Task SubmitHandle()
    {
        await _editRequestForm.Validate();

    //if (Key != null)
    //{
    //    var result = await DataSourceService.UpdateDataSource(Key.Value, _editRequestDto);

    //    if (result.Succeeded && MudDialog != null)
    //    {
    //        MudDialog.Close(DialogResult.Ok(true));
    //    }
    //}
    //else
    //{
    //    var result = await DataSourceService.CreateDataSource(_editRequestDto);

    //    if (result.Succeeded && MudDialog != null)
    //    {
    //        MudDialog.Close(DialogResult.Ok(true));
    //    }
    //}
    }

    protected override async Task OnInitializedAsync()
    {
        var result = await DatabaseService.QueryDatabases(new DatabaseSearchRequestDto());
        if (result.Data != null)
        {
            _databases = result.Data.ToList();
        }

        await base.OnInitializedAsync();
    }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new DataSourceEditRequestDto();
        if (Key != null)
        {
            var result = await DataSourceService.GetDataSource(Key.Value);
            if (result.Succeeded && result.Data != null)
            {
                _editRequestDto.Sort = result.Data.Sort;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

}